<?php

namespace App\Manage;


use App\Models\Course;
use App\Service\Base\Manage;

class CourseStudentManage extends Manage
{

    // 课程学员
    public function initialize()
    {
        parent::initialize();

        $this->model = 'CourseStudent';
    }

    // 列表
    // 添加
    // 修改
    // 软删除


    // 列表查询
    protected function listWhere()
    {
        $option  = $this->option();
        $where   = [];
        $bang     = [];
        $c_id    = $option->getData('course_id', 0, 'intval');
        $uid     = $option->getData('uid', 0, 'intval');
        $s_id    = $option->getData('student_id', 0, 'intval');
        $name    = $option->getData('name', null, 'trim');
        $keyword = $option->getData('keyword', null, 'trim');
        if($c_id){
            $bang[]  = 'course_id';
            $where[] = ['course_id', '=', $c_id];
        }
        if($uid){
            $bang[]  = 'uid';
            $where[] = ['uid', '=', $uid];
        }
        if($s_id){
            $bang[]  = 'student_id';
            $where[] = ['student_id', '=', $s_id];
        }
        if($name){
            $bang    = false; // 设置不在缓存
            $where[] = ['name', 'like', "%".$name."%"];
        }
        if($keyword){
            $bang    = false; // 设置不在缓存
            //$where[] = $this->orWhere('username|nickname|mobile', 'like', $keyword);
        }

          // 缓存
          $this->bang = $bang;

        return $where;
    }


    // 添加之前
    protected function _before_add()
    {
        $opt  = $this->option();
        $c_id = $opt->getData("course_id", null, 'intval');
        if(empty($c_id)){
            return $this->_returnFail('请选择课程！');
        }
        // 获取课程信息
        $cInfo = (new Course())->isCache(true)->oneFind(['id' => $c_id]);
        if(empty($cInfo)){
            return $this->_returnFail('选择的课程不存在！');
        }

        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            'uid'        => $cInfo['uid'],
            'course_id'  => $c_id,
            'student_id'  => $opt->getData("student_id", null, "intval"),
            'course_uid'  => $opt->getData("course_uid", null, "intval"),
            'person_name' => $opt->getData("person_name", null, "trim"),
            'mobile'      => $opt->getData("mobile", null, "trim"),
            'status'      => $opt->getData("status", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }

    // 修改之前
    protected function _before_edit()
    {
        $opt  = $this->option();
        $c_id = $opt->getData("course_id", null, 'intval');
        if(empty($c_id)){
            return $this->_returnFail('请选择课程！');
        }

        $data = [
            //'id'          => $opt->getData("id", null, "intval"),
            //'uid'        => $opt->getData("uid", null, 'intval'),
            'course_id'  => $c_id,
            'student_id'  => $opt->getData("student_id", null, "intval"),
            'course_uid'  => $opt->getData("course_uid", null, "intval"),
            'person_name' => $opt->getData("person_name", null, "trim"),
            'mobile'      => $opt->getData("mobile", null, "trim"),
            'status'      => $opt->getData("status", null, "intval"),
            //'ctime'       => $opt->getData("ctime", null, "intval"),
            //'mtime'       => $opt->getData("mtime", null, "intval"),
        ];

        return $data;
    }


}


